package app.transmission.remote.activity;

import com.google.inject.Inject;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import app.transmission.remote.R;
import app.transmission.remote.common.TranSettings;
import app.transmission.remote.connect.DaemonConnect;

@ContentView(R.layout.activity_start)
public class StartActivity extends RoboActivity {
	
	@InjectView(R.id.start_host)
	private EditText hostText;
	
	@InjectView(R.id.start_port)
	private EditText portText;
	
	@InjectView(R.id.start_user)
	private EditText userText;
	
	@InjectView(R.id.start_password)
	private EditText passwordText;
	
	@InjectView(R.id.start_remember)
	private CheckBox rememberBox;
	
	private TranSettings settings;
	
	@Inject
	private DaemonConnect daemonConnect;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		settings = (TranSettings) this.getApplication();
		hostText.setText(settings.getHost());
		portText.setText(settings.getPort());
		userText.setText(settings.getUser());
		passwordText.setText(settings.getPassword());
		rememberBox.setChecked(settings.isRemember());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.start, menu);
		return true;
	}
	
	public void connect(View v) {
		settings.setHost(hostText.getText().toString().trim());
		settings.setPort(portText.getText().toString().trim());
		settings.setUser(userText.getText().toString().trim());
		settings.setPassword(passwordText.getText().toString().trim());
		if (rememberBox.isChecked())
			settings.saveSettings();
		daemonConnect.startConnect();
		this.startActivity(new Intent(StartActivity.this, MainActivity.class));
		finish();
	}

}
